﻿
using System;
using System.IO;
using System.Threading.Tasks;
namespace Tradukisto.Engine.Utilities
{
    internal class SimpleLogger
    {
        private const string logTemplate = "{0} | {1} |{2}\r\n";
        private static readonly string logFolder = AppDomain.CurrentDomain.BaseDirectory;

        private static object locker = new object();

        public static void WriteInfo(string text)
        {
            var message = string.Format(logTemplate, DateTime.Now.ToString("yyy/MM/dd HH:mm:ss.fff"), "info", text);
            Task.Factory.StartNew(() => WriteToLog(message));
        }

        public static void WriteWarning(string text)
        {
            var message = string.Format(logTemplate, DateTime.Now.ToString("yyy/MM/dd HH:mm:ss.fff"), "warn", text);
            Task.Factory.StartNew(() => WriteToLog(message));
        }

        public static void WriteError(string text)
        {
            var message = string.Format(logTemplate, DateTime.Now.ToString("yyy/MM/dd HH:mm:ss.fff"), "erro", text);
            Task.Factory.StartNew(() => WriteToLog(message));
        }

        private static void WriteToLog(string text)
        {
            lock (locker)
            {
                if (!Directory.Exists(Path.Combine(logFolder, "LocalizationLogs")))
                {
                    Directory.CreateDirectory(Path.Combine(logFolder, "LocalizationLogs"));
                }
                File.AppendAllText(Path.Combine(logFolder, "LocalizationLogs", "Log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"), text);
            }

        }
    }
}
